.extern main
.equ GPBCON,              0x56000010
.equ GPBDAT,              0x56000014
    
.text
.global _start
_start:
    @ setup interrupt handler vector
    b   Reset
HandleUndef: 
    b   HandleUndef
HandleSwi: 
    b   HandleSwi
HandlePrefetchAbort: 
    b   HandlePrefetchAbort
HandleDataAbort: 
    b   HandleDataAbort
HandleNotUsed: 
    b   HandleNotUsed
    b   HandleIRQ
HandleFIQ: 
    b   HandleFIQ

Reset:
    ldr sp, =4096         @ setup stack for c routine
    bl  disable_watch_dog
    
    msr cpsr_c, #0xd2     @ enter interrupt mode
    ldr sp, =3072

    msr cpsr_c, #0xdf     @ enter system mode
    ldr sp, =4096

    bl  init_led
    bl  init_irq

    msr cpsr_c, #0x5f     @ turn interrupt

    ldr lr, =halt_loop
    ldr pc, =main
   @bl main

halt_loop:
    b   halt_loop

HandleIRQ:
@   bl turn_on_led2
    sub lr, #4
    stmdb sp!, {r0-r12,lr}
    ldr lr, =int_return
    ldr pc, =eint_handle

int_return:
    ldmia sp!, {r0-r12,pc}^

turn_off_all_led:
    ldr r0, =GPBDAT
    mov r1, #0x000001e0
    str r1, [r0]
    mov pc, lr
    
turn_on_all_led: 
    ldr r0, =GPBDAT
    mov r1, #0x00000000
    str r1, [r0]
    mov pc, lr

turn_on_led0: 
    ldr r0, =GPBDAT
    mov r1, #0x000001d0
    str r1, [r0]
    mov pc, lr
    
turn_on_led1: 
    ldr r0, =GPBDAT
    mov r1, #0x000001b0
    str r1, [r0]
    mov pc, lr

turn_on_led2: 
    ldr r0, =GPBDAT
    mov r1, #0x00000170
    str r1, [r0]
    mov pc, lr

turn_on_led3: 
    ldr r0, =GPBDAT
    mov r1, #0x000000e0
    str r1, [r0]
    mov pc, lr
